#-*-coding:utf-8-*-
from django.shortcuts import render,HttpResponse,render_to_response,HttpResponseRedirect
from nav import models as nav_models
import json

# Create your views here.

def index(request):

    return render_to_response('index.html')
    #return render_to_response('demo/index.html')


def configNav(request,*args):
    '''
    :param request:
    :param args:    #接收URL参数:
                                add     : 添加导航栏
                                modify  : 修改导航栏
                                delete  : 删除导航栏
    :return:
    '''
    if request.method == 'POST':
        name = request.POST.get('name')
        order = request.POST.get('order')
        describe = request.POST.get('describe')
        id = request.POST.get('id')
        if args[0] == 'add':
            if nav_models.page_nav.objects.filter(name=name).count() == 0 :
                nav_models.page_nav.objects.create(
                    name = name,
                    order = order,
                    describe = describe
                )

        if args[0] == 'modify':
            print id
            nav_models.page_nav.objects.filter(id=id).update(
                name=name,
                order=order,
                describe=describe
            )

        if args[0] == 'delete':
            nav_models.page_nav.objects.filter(id=id).delete()

        return HttpResponseRedirect('/nav/configNav/')

    r_data = {}
    pageData = nav_models.page_nav.objects.all().order_by('order')

    #获致导航栏头
    if args[0] == "getNavHeader":
        nav_list = []
        for item in pageData:
            nav_list.append(item.name)
        return HttpResponse(json.dumps(nav_list))

    r_data['pageData'] = pageData

    return render_to_response('nav/configNav.html', r_data)

def configMenu(request,*args):
    if request.method == 'POST':
        id = request.POST.get('id')
        nav_id = request.POST.get('nav_id')
        name = request.POST.get('name')
        order = request.POST.get('order')
        describe = request.POST.get('describe')
        if nav_id:
            nav_obj = nav_models.page_nav.objects.get(id=nav_id)
        if args[0] == "add":
            if nav_models.page_menu.objects.filter(name=name).count() == 0:
                nav_models.page_menu.objects.create(
                    name = name,
                    order = order,
                    describe = describe,
                    nav = nav_obj
                )
        if args[0] == "modify":
            nav_models.page_menu.objects.filter(id=id).update(
                name=name,
                order=order,
                describe=describe,
                nav=nav_obj
            )
        if args[0] == "delete":
            print id
            nav_models.page_menu.objects.filter(id=id).delete()

        return HttpResponseRedirect('/nav/configMenu/')

    nav_id = request.GET.get('nav_id')
    if nav_id:
        table_data = []
        for item in nav_models.page_menu.objects.filter(nav__id=nav_id).order_by('nav__order','order'):
            table_data.append({
                'nav_id':item.nav.id,
                'id':item.id,
                'name':item.name,
                'order':item.order,
                'describe':item.describe,
                'nav_name':item.nav.name
            })
        return HttpResponse(json.dumps(table_data))


    r_data = {}
    pageData = nav_models.page_menu.objects.all().order_by('nav__order','order')
    navData = nav_models.page_nav.objects.all().order_by('order')
    r_data['pageData'] = pageData
    r_data['navData'] = navData

    return render_to_response('nav/configMenu.html',r_data)

def configItem(request,*args):
    if request.method == 'POST':
        id = request.POST.get('id')
        nav_id = request.POST.get('nav_id')
        menu_id = request.POST.get('menu_id')
        name = request.POST.get('name')
        order = request.POST.get('order')
        link = request.POST.get('link')
        if nav_id:
            nav_obj = nav_models.page_nav.objects.get(id=nav_id)
        if menu_id:
            menu_obj = nav_models.page_menu.objects.get(id=menu_id)
        if args[0] == "add":
            if nav_models.page_item.objects.filter(name=name).count()==0:
                nav_models.page_item.objects.create(
                    name = name,
                    order = order,
                    link = link,
                    menu = menu_obj
                )

        if args[0] == "modify":
            nav_models.page_item.objects.filter(id=id).update(
                name=name,
                order=order,
                link=link,
                menu=menu_obj
            )
        if args[0] == "delete":
            print id
            nav_models.page_item.objects.filter(id=id).delete()
        return HttpResponseRedirect("/nav/configItem")
    nav_id = request.GET.get('nav_id')
    menu_id = request.GET.get('menu_id')
    if nav_id:
        itemData = []
        for item in nav_models.page_item.objects.filter(menu__nav__id=nav_id).order_by('menu__nav__order','menu__order','order'):
            itemData.append({
                'id':item.id,
                'name':item.name,
                'order':item.order,
                'link':item.link,
                'menu_name':item.menu.name,
                'menu_id':item.menu.id,
                'nav_name':item.menu.nav.name,
                'nav_id':item.menu.nav.id
            })
        menuData=[]
        for item in nav_models.page_menu.objects.filter(nav__id=nav_id).order_by('nav__order','order'):
            menuData.append({'id':item.id,'name':item.name})
        return HttpResponse(json.dumps({'itemData':itemData,'menuData':menuData}))

    if menu_id:
        itemData = []
        for item in nav_models.page_item.objects.filter(menu__id=menu_id).order_by('menu__nav__order','menu__order','order'):
            itemData.append({
                'id': item.id,
                'name': item.name,
                'order': item.order,
                'link': item.link,
                'menu_name': item.menu.name,
                'menu_id': item.menu.id,
                'nav_name': item.menu.nav.name,
                'nav_id': item.menu.nav.id
            })
        nav_id = nav_models.page_menu.objects.get(id=menu_id).nav.id
        return HttpResponse(json.dumps({'itemData':itemData,'nav_id':nav_id}))

    r_data={}
    menuData = nav_models.page_menu.objects.all().order_by('nav__order','order')
    navData = nav_models.page_nav.objects.all().order_by('order')
    pageData = nav_models.page_item.objects.all().order_by('menu__nav__order','menu__order','order')
    r_data['pageData']=pageData
    r_data['menuData']=menuData
    r_data['navData']=navData

    return render_to_response('nav/configItem.html',r_data)

def getItem(request,*args):
    nav_name = request.GET.get('nav_name')
    itemData={}
    for item in nav_models.page_item.objects.filter(menu__nav__name=nav_name).order_by("menu__nav__order",'menu__order','order'):
        if not item.menu.name in itemData.keys():
            itemData[item.menu.name]=[]
        itemData[item.menu.name].append({
            'item_name':item.name,
            'item_link':item.link
        })
    return HttpResponse(json.dumps(itemData))